﻿using System.Collections.Generic;
using System.Linq;

namespace ElevatorApplication.Extensions
{
    public static class ButtonStatesExtensions
    {
        public static ButtonsOfFloor For(this List<ButtonsOfFloor> list, int floor)
        {
            return list.Single(x => x.FloorNumber == floor);
        }

        public static List<ButtonsOfFloor> Create(this int floor)
        {
            return Enumerable.Range(1, floor).Select(x => new ButtonsOfFloor(x)).ToList();
        }

        public static bool IsHigherFloorButtonBePushed(this IElevator elevator)
        {
            // ReSharper disable once ComplexConditionExpression
            var clicked = elevator.ButtonStateInFloors
                .Where(x => x.FloorNumber > elevator.CurrentFloorNumber)
                .Any(x => x.IsUpButtonBePushed || x.IsNumberButtonBePushed);

            return clicked;
        }

        public static int GetNextHigherBePushedFloor(this IElevator elevator)
        {
            var floor = elevator.ButtonStateInFloors
                .Where(x => x.FloorNumber > elevator.CurrentFloorNumber)
                .First();

            return floor.FloorNumber;
        }
    }
}
